/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.TuyenTau_DTO;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Phung
 */
public class TuyenTau_DAO {
    
    public static String Get_MaTT() throws SQLException{
        String MaTT = null;
        MySQLDataHelper_DAO.open();
        Connection Con = MySQLDataHelper_DAO.getConnection();
        CallableStatement statement = Con.prepareCall("{call Add_CT_ID(?)}");
        statement.setString(1, "CT");
        statement.execute();
        ResultSet Rs = statement.getResultSet();
        if(Rs.next()){
            MaTT = Rs.getString(1);
        }
        return MaTT;
    }
    
    public static ArrayList<TuyenTau_DTO> GetALL()
    {
        ArrayList<TuyenTau_DTO> ds = new ArrayList<>();
        try {
            String sql = "Select * from TuyenTau" ;
            MySQLDataHelper_DAO.open();
            ResultSet rs =  MySQLDataHelper_DAO.executeQuery(sql);
            while (rs.next()) {
                TuyenTau_DTO TuyenTau = new TuyenTau_DTO();
                TuyenTau.setMaTuyen(rs.getString("MaTuyen"));
                TuyenTau.setTenTuyen(rs.getString("TenTuyen"));
                TuyenTau.setSoTuyen(rs.getInt("SoTuyen"));
                ds.add(TuyenTau);
            }
            MySQLDataHelper_DAO.close();
       }catch (SQLException ex) {
            System.out.println(ex);
        }
        return ds;
    }
    
    public static boolean Insert(TuyenTau_DTO TuyenTau)
    {
        boolean flag = false;
        try {
            String sql = "Insert TuyenTau Values('"+TuyenTau.getMaTuyen()+"','"+TuyenTau.getTenTuyen()+"','"+TuyenTau.getSoTuyen()+"')";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Update(TuyenTau_DTO TuyenTau)
    {
        boolean flag = false;
        try {
            String sql = "Update TuyenTau Set TenTuyen = '"+ TuyenTau.getTenTuyen() +"', SoTuyen = "+TuyenTau.getSoTuyen()+ " where MaTuyen = '"+ TuyenTau.getMaTuyen()+"'";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Delete(String maTT)
    {
        boolean flag = false;
        try {
            String sql = "Delete from TuyenTau where MaTuyen = '"+maTT+"'";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
}
